﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace StudentManagement
{
    public partial class Form_hometeacher : Form
    {
        public String teacherName, formerID;
        public Form_hometeacher(String name)
        {
            InitializeComponent();
            this.teacherName = name;
            showTeacherInfo();
            showStudentList1();
            showStudentList2();
            showStudentList3();
        }


        //连接数据库
        public MySqlConnection connectionF()
        {
            String connStr = "server=localhost;user id=root;Password=wutong;persist security info=True;database=sms";
            MySqlConnection conn = new MySqlConnection(connStr);
            try
            {
                conn.Open();
            }
            catch (System.Exception e)
            {
                MessageBox.Show("无法打开数据库！\n" + e.Message, "错误提示",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                return null;
            }
            return conn;
        }





        /********教师个人信息界面***********/

        //显示班主任的教师号、姓名、办公室、电话、担任班级以及个人简介
        private void showTeacherInfo()
        {

            MySqlConnection conn = connectionF();
            String cmdStr = "select * from teacher_info where ID='" + teacherName + "'";
            MySqlCommand cmd = new MySqlCommand(cmdStr, conn);
            MySqlDataReader dr = cmd.ExecuteReader();
            if (dr.Read())
            {
                textBox1.Text = teacherName;
                textBox3.Text = dr["name"].ToString();
                textBox4.Text = dr["office"].ToString();
                textBox5.Text = dr["tele_no"].ToString();
                richTextBox1.Text = dr["description"].ToString();
            }
            dr.Close();
            String cmdStr1 = "select * from class_lead where ID='" + teacherName + "'";
            MySqlCommand cmd1 = new MySqlCommand(cmdStr1, conn);
            MySqlDataReader dr1 = cmd1.ExecuteReader();
            if (dr1.Read())
            {
                textBox7.Text = dr1["class"].ToString();
            }

            else
                MessageBox.Show("打开个人信息错误！", "错误",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
            if (!dr1.IsClosed)
                dr1.Close();
            if (conn.State.Equals(ConnectionState.Open))
                conn.Close();

        }

        //*************查看本班学生信息界面***************//

        //将学生的学号添加到修改界面中
        private void showStudentList1()
        {
            String cmdStr = "select ID from student where class='" + textBox7.Text + "'";
            MySqlConnection conn = connectionF();
            MySqlCommand cmd = new MySqlCommand(cmdStr, conn);
            MySqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())   //学生学号
                listBox1.Items.Add(dr["ID"].ToString());

            if (!dr.IsClosed)
                dr.Close();
            if (conn.State.Equals(ConnectionState.Open))
                conn.Close();
        }


        //将被选中的学生学号、学生姓名显示出来
        private void listBox1_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            textBox11.Text = listBox1.SelectedItem.ToString();
            MySqlConnection conn = connectionF();
            String cmdStr = "select * from student where ID='" + textBox11.Text + "'";
            MySqlCommand cmd = new MySqlCommand(cmdStr, conn);
            MySqlDataReader dr = cmd.ExecuteReader();
            if (dr.Read())
            {
                textBox12.Text = dr["name"].ToString();
                textBox13.Text = dr["sex"].ToString();
                textBox14.Text = dr["class"].ToString();
                textBox15.Text = dr["department"].ToString();
                textBox16.Text = dr["birthday"].ToString();
            }
            else
                MessageBox.Show("打开学生详细信息失败，请重试！", "错误",
                        MessageBoxButtons.OK, MessageBoxIcon.Error);
            if (!dr.IsClosed)
                dr.Close();
            if (conn.State.Equals(ConnectionState.Open))
                conn.Close();
            formerID = textBox1.Text.ToString();
        }





        //**************查看学生个人成绩界面*******************//




        //将学生的学号列在表中
        private void showStudentList2()
        {
            String cmdStr = "select ID from student where class='" + textBox7.Text + "'";
            MySqlConnection conn = connectionF();
            MySqlCommand cmd = new MySqlCommand(cmdStr, conn);
            MySqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())   //学生学号
            {
                listBox2.Items.Add(dr["ID"].ToString());
            }
            if (!dr.IsClosed)
                dr.Close();
            if (conn.State.Equals(ConnectionState.Open))
                conn.Close();
        }




        //将被选中的学生学号、学生姓名显示出来
        private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            textBox9.Text = listBox2.SelectedItem.ToString();
            MySqlConnection conn = connectionF();
            String cmdStr = "select * from student where ID='" + listBox2.SelectedItem.ToString() + "'";
            MySqlCommand cmd = new MySqlCommand(cmdStr, conn);
            MySqlDataReader dr = cmd.ExecuteReader();
            if (dr.Read())
            {
                textBox2.Text = dr["name"].ToString();
            }
            else
                MessageBox.Show("打开学生详细信息失败，请重试！", "错误",
                        MessageBoxButtons.OK, MessageBoxIcon.Error);
            if (!dr.IsClosed)
                dr.Close();
            if (conn.State.Equals(ConnectionState.Open))
                conn.Close();
            showStudentList3();
        }


        //*****************按课程查看学生成绩界面*************//
                //将所有课程名列在表中

        private void showStudentList3()
        {
            MySqlConnection conn = connectionF();
            String cmdStr = "select lesson.name as name from lesson_choice,lesson where sno='"+ textBox9.Text +"' and lesson_choice.lesson_ID=lesson.ID ";
            MySqlCommand cmd = new MySqlCommand(cmdStr, conn);
            MySqlDataReader dr = cmd.ExecuteReader();

            listBox3.Items.Clear();

            while (dr.Read())   //课程名
            {
                listBox3.Items.Add(dr["name"].ToString());
            }
             if (!dr.IsClosed)
                dr.Close();
            if (conn.State.Equals(ConnectionState.Open))
                conn.Close();

        }


        //将被选中的学生成绩显示出来
        private void listBox3_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            textBox6.Text=listBox3.SelectedItem.ToString();
            MySqlConnection conn = connectionF();
            String cmdStr = "select score from student,lesson_choice,lesson where class='" + textBox7.Text + "' and lesson_choice.lesson_ID=lesson.ID and lesson_choice.sno=student.ID  and lesson_choice.sno='" + listBox2.SelectedItem.ToString() + "'";
            MySqlCommand cmd = new MySqlCommand(cmdStr, conn);
            MySqlDataReader dr = cmd.ExecuteReader();
           if(dr.Read())
           {
               if (!dr["score"].ToString().Equals("-1"))
                   textBox8.Text = dr["score"].ToString();
               else
                   textBox8.Text = "成绩未评定";
            }
            else
                MessageBox.Show("打开学生成绩失败，请重试！", "错误",
                        MessageBoxButtons.OK, MessageBoxIcon.Error);
            if (!dr.IsClosed)
                dr.Close();
            if (conn.State.Equals(ConnectionState.Open))
                conn.Close();
        }


        //修改密码
        private void button1_Click_1(object sender, EventArgs e)
        {
            Form_chPassword changeP = new Form_chPassword(teacherName, "teacher_info");
            changeP.Show();
        }


        private void Form_hometeacher_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“smsDataSet4.scorebylesson”中。您可以根据需要移动或移除它。
          
            this.lesson_choiceTableAdapter.Fill(this.smsDataSet1.lesson_choice);

        }

        private void button2_Click(object sender, EventArgs e)
        {
            MySqlConnection conn = connectionF();
            String cmdStr = "update teacher_info set tele_no='" +
                textBox5.Text + "', office='" +
                textBox4.Text + "' where ID='" +
                teacherName + "'";
            MySqlCommand cmd = new MySqlCommand(cmdStr, conn);
            if (cmd.ExecuteNonQuery() == 1)
                MessageBox.Show("信息修改成功！请点击继续···", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
            else
                MessageBox.Show("修改个人信息错误！", "错误",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);

            if (conn.State.Equals(ConnectionState.Open))
                conn.Close();

        }

    }
}